package top.starmc.starpet.util

import github.saukiya.sxattribute.SXAttribute
import org.bukkit.entity.Player
import org.serverct.ersha.api.AttributeAPI
import top.starmc.starpet.config.Config
import top.starmc.starpet.internal.PlayerData
import java.util.*

val playerMap = hashMapOf<UUID, PlayerData>()

fun Player.loadPlayerData() {
    playerMap[uniqueId] = PlayerData(this)
}

fun Player.getPlayerData(delete: Boolean = false): PlayerData {
    val result = playerMap[uniqueId]!!
    if (delete) {
        playerMap -= uniqueId
    }
    return result
}

fun Player.updateStats(key: Class<out Any>, stats: List<String>) {
    when (Config.rpgPlugin) {
        1 -> {
            SXAttribute.getApi().setEntityAPIData(key, uniqueId, SXAttribute.getApi().getLoreData(null, null, stats))
            SXAttribute.getApi().updateStats(this)
        }

        2 -> {
            AttributeAPI.addSourceAttribute(AttributeAPI.getAttrData(this), key.simpleName, stats)
        }
    }
}

fun Player.removeStats(key: Class<out Any>) {
    when (Config.rpgPlugin) {
        1 -> {
            SXAttribute.getApi().removeEntityAPIData(key, uniqueId)
            SXAttribute.getApi().updateStats(this)
        }

        2 -> {
            AttributeAPI.takeSourceAttribute(AttributeAPI.getAttrData(this), key.simpleName)
        }
    }
}